package com.shanghaionstar.carmanage.chargpile;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.UiSettings;
import com.onstar.cn.auth.userProfile.FeatureEnum;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.VehicleLoadingActivity;
import com.shanghaionstar.carmanage.ChargingPileActivity;
import com.shanghaionstar.carmanage.iview.IChargingPileLocationView;
import com.shanghaionstar.carmanage.model.ChargePileStationEntity;
import com.shanghaionstar.carmanage.presenter.ChargingPileLocationPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * User: jun chen
 * Class:　ChargingPileStationLocationFragment
 * Date: 2016-06-13  15:52
 * Description:充电桩站点位置页面
 */
public class ChargePileStationLocationFragment extends BaseFragment implements IChargingPileLocationView{

    @FindViewById(R.id.mapchoosing_point_headview)
    private TopLayout mapchoosing_point_headview;
    @FindViewById(R.id.mapview)
    private OMapView mapview;

    @FindViewById(R.id.text_StationName)
    private TextView text_StationName;//充点电名称
    @FindViewById(R.id.text_station_address)
    private TextView text_station_address;//充电桩地址

    //音控导航
    @FindViewById(R.id.btn_send_tbt_view)
    private RelativeLayout btn_send_tbt_view;
    @FindViewById(R.id.btn_send_tbt)
    private ImageButton btn_send_tbt;
    //车载导航
    @FindViewById(R.id.btn_send_odd_view)
    private RelativeLayout btn_send_odd_view;
    @FindViewById(R.id.btn_send_odd)
    private ImageButton btn_send_odd;

    @FindViewById(R.id.map_bottomview)
    private LinearLayout map_bottomview;

    //快充桩个数
    @FindViewById(R.id.fast_charge_content)
    private TextView fast_charge_content;
    //慢充桩个数
    @FindViewById(R.id.trickle_charge_content)
    private TextView trickle_charge_content;
    //空闲桩个数
    @FindViewById(R.id.free_station_content)
    private TextView free_station_content;

    @FindViewById(R.id.free_station)
    private TextView free_station;

    private ChargingPileLocationPresenter presenter;
    private ChargingPileActivity parentActivity;

    private UiSettings mUiSettings;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btnBackClick(mapchoosing_point_headview);
        if(isFirstLoad()) {
            mapview.onCreate(savedInstanceState);
            setMap();
            parentActivity = (ChargingPileActivity) getActivity();
            presenter = new ChargingPileLocationPresenter(parentActivity.getScheduler());
            presenter.setView(this);
            presenter.initMapHandler(mapview.getMap());
        }
        presenter.initData((ChargePileStationEntity) getArguments().get(Constants.CHARGEPILEINFO));
    }

    private void setMap(){
        mUiSettings = mapview.getMap().getUiSettings();
        //隐藏地图自带的缩放控件
        mUiSettings.setZoomControlsEnabled(true);
        //设置可通过手势缩放
        mUiSettings.setZoomGesturesEnabled(true);
        //指南针
        mUiSettings.setCompassEnabled(false);
    }

    @Override
    public int getLayoutId() {
        return R.layout.charging_pile_station_location;
    }


    @OnClick({R.id.btn_send_tbt, R.id.btn_send_odd, R.id.view1, R.id.text_StationName, R.id.text_station_address})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.btn_send_tbt:
                presenter.sendTbtOrOdd("TBT");
                break;
            case R.id.btn_send_odd:
                presenter.sendTbtOrOdd("ODD");
                break;
            //跳转到站点详情页面
            case R.id.view1:
            case R.id.text_StationName:
            case R.id.text_station_address:
                presenter.switchToDetail();
                break;
        }
    }


    @SuppressWarnings("ConstantConditions")
    private void updateTbtOddBtnStatus() {
        //音控导航 TBT
        if (ApplicationUtil.getInstance().isLogin()) {
            btn_send_tbt_view.setVisibility(View.VISIBLE);
        }

        // 车载导航 ODD
        if (!ApplicationUtil.getInstance().isLogin() || !BusinessUtil.isServiceFeature(
                FeatureEnum.SENDNAVDESTINATION.value(), parentActivity)) {
            btn_send_odd_view.setVisibility(View.GONE);
        } else {
            btn_send_odd_view.setVisibility(View.VISIBLE);
        }
    }



    @Override
    public void onResume() {
        super.onResume();
        updateTbtOddBtnStatus();
        mapview.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapview.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapview.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapchoosing_point_headview = null;
        mapview.onDestroy();
        mapview = null;
    }

    @Override
    public void setLoading(boolean isLoading) {

    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(),e);
    }

    @Override
    public void onReponseSuccess(List<ChargePileStationEntity> object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void initView(ChargePileStationEntity chargePileStationEntity) {
        map_bottomview.setVisibility(View.VISIBLE);
        text_StationName.setText(chargePileStationEntity.getStationName());
        text_station_address.setText(chargePileStationEntity.getAddress());
        //快充桩个数
        if (CommonUtil.isNull(chargePileStationEntity.getQuickCharge())){
            chargePileStationEntity.setQuickCharge("0");
        }
        fast_charge_content.setText(chargePileStationEntity.getQuickCharge());
        //慢充装个数
        if (CommonUtil.isNull(chargePileStationEntity.getSlowCharge())){
            chargePileStationEntity.setSlowCharge("0");
        }
        trickle_charge_content.setText(chargePileStationEntity.getSlowCharge());
        //空闲桩
        if (!getString(R.string.poi_not_city_str).equals(chargePileStationEntity.getStatus()))
            free_station_content.setText(chargePileStationEntity.getStatus());
        else {
            free_station_content.setVisibility(View.INVISIBLE);
            free_station.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showCommandLoading(boolean show) {
        if(show) {
            startActivity(new Intent(parentActivity, VehicleLoadingActivity.class));
        }
    }

    /**
     *
     * 发送tbt失败重试toast
     */
    @Override
    public void showAppFrontNavCommandFailToast(final PoiItemInfo poiItemInfo) {
        AlertDialogUtil.showAlertDialog(parentActivity, getString(R.string.dialog_warning), getString(R.string.nav_command_fail_alert), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.sendTbtOrOdd(poiItemInfo);
            }
        });
    }

    /**
     * app在后端时候，发送tbt失败提示toast
     * @param poiItemInfo
     */
    @Override
    public void showAppBackNavCommandFailToast(final PoiItemInfo poiItemInfo) {
        ToastView toastView = new ToastView(parentActivity);
        toastView.setToastClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAppFrontNavCommandFailToast(poiItemInfo);
            }
        });
    }
}